package com.haois.mp.database.module.wxserver.service;

import com.haois.mp.database.module.common.daomain.Parameter;
import com.haois.mp.database.module.wxserver.dao.WxMpMapper;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * description
 *
 * @author RuiHaoZ on hgits.cn
 * @date 2018/10/31 10:42
 */
@Service
public class EventHandlerService {

    @Autowired
    private WxMpMapper wxMpMapper;


    public void saveEvent(WxMpXmlMessage wxMpXmlMessage) {
        wxMpXmlMessage.setCreateTime(new Date().getTime());
        wxMpMapper.saveClickEvent(wxMpXmlMessage);
    }

    public void updateEvent(WxMpXmlMessage wxMpXmlMessage) {
        wxMpXmlMessage.setCreateTime(new Date().getTime());
        wxMpMapper.updateClickEvent(wxMpXmlMessage);
    }

    public void freshenEvent(WxMpXmlMessage wxMpXmlMessage) {
        this.deleteNoEvent(wxMpXmlMessage.getFromUser(), wxMpXmlMessage.getEventKey());
        boolean exist = this.existEvent(wxMpXmlMessage.getFromUser(),
                wxMpXmlMessage.getEventKey());
        if (exist) {
            this.updateEvent(wxMpXmlMessage);
        } else {
            this.saveEvent(wxMpXmlMessage);
        }
    }

    public void freshenEvent(String fromUser, String eventKey, String event) {
        this.deleteNoEvent(fromUser, eventKey);
        boolean exist = this.existEvent(fromUser,
                eventKey);
        if (exist) {
            this.updateEvent(fromUser, eventKey, event);
        } else {
            this.saveEvent(fromUser, eventKey, event);
        }
    }

    public void saveEvent(String fromUser, String eventKey, String event) {
        WxMpXmlMessage wxMpXmlMessage = new WxMpXmlMessage();
        wxMpXmlMessage.setFromUser(fromUser);
        wxMpXmlMessage.setEventKey(eventKey);
        wxMpXmlMessage.setEvent(event);
        wxMpXmlMessage.setCreateTime(new Date().getTime());
        wxMpMapper.saveClickEvent(wxMpXmlMessage);
    }

    public void updateEvent(String fromUser, String eventKey, String event) {
        WxMpXmlMessage wxMpXmlMessage = new WxMpXmlMessage();
        wxMpXmlMessage.setFromUser(fromUser);
        wxMpXmlMessage.setEventKey(eventKey);
        wxMpXmlMessage.setEvent(event);
        wxMpXmlMessage.setCreateTime(new Date().getTime());
        wxMpMapper.updateClickEvent(wxMpXmlMessage);
    }

    public void deleteNoEvent(String fromUserName, String eventKey) {
        wxMpMapper.deleteNoClickEvent(fromUserName, eventKey);
    }

    public void deleteEvent(String fromUserName, String eventKey) {
        wxMpMapper.deleteClickEvent(fromUserName, eventKey);
    }

    public void deleteEvent(String fromUserName) {
        wxMpMapper.deleteClickEventByUser(fromUserName);
    }

    public boolean existEvent(String fromUserName, String eventKey) {
        WxMpXmlMessage wxMp = wxMpMapper.queryQuickEvent(fromUserName, eventKey);
        boolean inTime = this.eventInTime(wxMp);
        return inTime;
    }

    public boolean eventInTime(WxMpXmlMessage wxMpXmlMessage) {
        if (wxMpXmlMessage != null) {
            long time = new Date().getTime() - wxMpXmlMessage.getCreateTime();
            if (time > (1000 * 60 * 5)) {
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

    public String queryLatelyEvent(String fromUser) {
        WxMpXmlMessage wxMp = wxMpMapper.queryEventByUser(fromUser);
        boolean inTime = this.eventInTime(wxMp);
        if (!inTime && wxMp != null) {
            wxMpMapper.deleteClickEventByUser(fromUser);
        }
        if (!inTime) {
            return "";
        } else {
            return wxMp.getEventKey();
        }
    }

    public List<Parameter> getParameter(String eventKey) {
        return wxMpMapper.getParameter(eventKey);
    }

    public Parameter getParameter(String xh, String eventKey) {
        return wxMpMapper.getParameterByXh(xh, eventKey);
    }

    public Parameter getParByContAndKey(String content, String eventKey) {
        return wxMpMapper.getParByContAndKey(content, eventKey);
    }
}
